using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using QL_Truong_Hoc.DTO;

namespace QL_Truong_Hoc.DAO
{
	public static class MLopDAO
	{
		#region Methods

		/// <summary>
		/// Saves a record to the mLop table.
		/// </summary>
		public static bool Insert(Database objDb, DbTransaction objTrans, MLopDTO mLop)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mLop", mLop);

			object[] parameters = new object[]
			{
				Null.GetNull(mLop.KDT_ID, DBNull.Value),
				Null.GetNull(mLop.Lop_ten, ""),
				Null.GetNull(mLop.Lop_batdau, DBNull.Value),
				Null.GetNull(mLop.Lop_ketthuc, DBNull.Value),
				Null.GetNull(mLop.Lop_diadiem, ""),
				Null.GetNull(mLop.Lop_chungchi, ""),
				Null.GetNull(mLop.Lop_quyetdinh, ""),
				Null.GetNull(mLop.Lop_quyetdinh_ngay, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mLopInsert", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mLopInsert", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mLopInsert", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Updates a record in the mLop table.
		/// </summary>
		public static bool Update(Database objDb, DbTransaction objTrans, MLopDTO mLop)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mLop", mLop);

			object[] parameters = new object[]
			{
				Null.GetNull(mLop.Lop_ID, DBNull.Value),
				Null.GetNull(mLop.KDT_ID, DBNull.Value),
				Null.GetNull(mLop.Lop_ten, ""),
				Null.GetNull(mLop.Lop_batdau, DBNull.Value),
				Null.GetNull(mLop.Lop_ketthuc, DBNull.Value),
				Null.GetNull(mLop.Lop_diadiem, ""),
				Null.GetNull(mLop.Lop_chungchi, ""),
				Null.GetNull(mLop.Lop_quyetdinh, ""),
				Null.GetNull(mLop.Lop_quyetdinh_ngay, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mLopUpdate", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mLopUpdate", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mLopUpdate", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mLop table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, int lop_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(lop_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mLopDelete", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mLopDelete", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mLopDelete", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mLop table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, string strWhere)		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{strWhere
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mLopDeleteWhere", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mLopDeleteWhere", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mLopDeleteWhere", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Selects a single record from the mLop table.
		/// </summary>
		public static MLopDTO Select(Database objDb, int lop_ID)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(lop_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mLopSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects a single record from the mLop table.
		/// </summary>
		public static DataTable Select(Database objDb, int lop_ID, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(lop_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mLopSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Get next index from the mLop table.
		/// </summary>
		public static int SelectNextIndex(Database objDb)		{
int intResult = -1;
try{
IDataReader dr = objDb.ExecuteReader("mLopSelectNextIndex");
if (dr != null)
{
DataTable dt = Untils.ConvertDataReaderToDataTable(dr, true);
intResult = clsCommon.fncCnvNullToInt(dt.Rows[0][0]);
}
}catch { }
return intResult;
		}

		/// <summary>
		/// Selects all records from the mLop table.
		/// </summary>
		public static List<MLopDTO> SelectAll(Database objDb)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mLopSelectAll"))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mLop table.
		/// </summary>
		public static List<MLopDTO> SelectWhere(Database objDb, string strWhere)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mLopSelectWhere", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mLop table.
		/// </summary>
		public static DataTable SelectAll(Database objDb, bool blnReturnDatatable = true)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mLopSelectAll"))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mLop table.
		/// </summary>
		public static DataTable SelectWhere(Database objDb, string strWhere, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mLopSelectWhere", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Creates a new instance of the MLopDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static MLopDTO MapDataReader(IDataReader dataReader)
		{
			MLopDTO mLopDTO = new MLopDTO();
DataTable dt = new DataTable();
dt.Load(dataReader);
if (dt.Rows.Count <= 0) return mLopDTO;
			mLopDTO.Lop_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["Lop_ID"]);
			mLopDTO.KDT_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["KDT_ID"]);
			mLopDTO.Lop_ten = clsCommon.fncCnvNullToString(dt.Rows[0]["Lop_ten"]);
			mLopDTO.Lop_batdau = clsCommon.fncCnvNullToDatetime(dt.Rows[0]["Lop_batdau"]);
			mLopDTO.Lop_ketthuc = clsCommon.fncCnvNullToDatetime(dt.Rows[0]["Lop_ketthuc"]);
			mLopDTO.Lop_diadiem = clsCommon.fncCnvNullToString(dt.Rows[0]["Lop_diadiem"]);
			mLopDTO.Lop_chungchi = clsCommon.fncCnvNullToString(dt.Rows[0]["Lop_chungchi"]);
			mLopDTO.Lop_quyetdinh = clsCommon.fncCnvNullToString(dt.Rows[0]["Lop_quyetdinh"]);
			mLopDTO.Lop_quyetdinh_ngay = clsCommon.fncCnvNullToDatetime(dt.Rows[0]["Lop_quyetdinh_ngay"]);

			return mLopDTO;
		}
		/// <summary>
		/// Creates a new instance of the MLopDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static List<MLopDTO> MapDataReaderList(IDataReader dataReader)
		{
				List<MLopDTO> lstResult = new List<MLopDTO>();
			MLopDTO mLopDTO;
DataTable dt = new DataTable();
dt.Load(dataReader);
for (int i = 0; i <= dt.Rows.Count-1; i++)
{
			mLopDTO = new MLopDTO();
			mLopDTO.Lop_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["Lop_ID"]);
			mLopDTO.KDT_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["KDT_ID"]);
			mLopDTO.Lop_ten = clsCommon.fncCnvNullToString(dt.Rows[i]["Lop_ten"]);
			mLopDTO.Lop_batdau = clsCommon.fncCnvNullToDatetime(dt.Rows[i]["Lop_batdau"]);
			mLopDTO.Lop_ketthuc = clsCommon.fncCnvNullToDatetime(dt.Rows[i]["Lop_ketthuc"]);
			mLopDTO.Lop_diadiem = clsCommon.fncCnvNullToString(dt.Rows[i]["Lop_diadiem"]);
			mLopDTO.Lop_chungchi = clsCommon.fncCnvNullToString(dt.Rows[i]["Lop_chungchi"]);
			mLopDTO.Lop_quyetdinh = clsCommon.fncCnvNullToString(dt.Rows[i]["Lop_quyetdinh"]);
			mLopDTO.Lop_quyetdinh_ngay = clsCommon.fncCnvNullToDatetime(dt.Rows[i]["Lop_quyetdinh_ngay"]);
lstResult.Add(mLopDTO);
}

			return lstResult;
		}

		#endregion
	}
}
